package com.sdehua.wear.controller;

import com.sdehua.wear.entity.CartVO;
import com.sdehua.wear.entity.Myorder;
import com.sdehua.wear.service.MyorderService;
import com.sdehua.wear.utils.ResultEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * (Myorder)表控制层
 *
 * @author makejava
 * @since 2021-12-31 15:29:41
 */
@RestController
@RequestMapping("myorder")
@CrossOrigin
public class MyorderController {
    /**
     * 服务对象
     */
    @Resource
    private MyorderService myorderService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Myorder selectOne(Integer id) {
        return this.myorderService.queryById(id);
    }

    @RequestMapping("addOrder")
    public ResultEntity addOrder(String userId,String goodsId){
        boolean flag = myorderService.addOrder(userId,goodsId);
        if (flag){
            return ResultEntity.fail("添加购物袋失败");
        }
        return ResultEntity.success("成功添加至购物袋");
    }

    @RequestMapping("queryAllByUserId")
    public ResultEntity queryAllByUserId(String userId){
        List<CartVO> list =  myorderService.queryAllByUserId(userId);
        if (Objects.equals(list,null)){
            return ResultEntity.fail("查询失败");
        }
        return ResultEntity.success(list);
    }

    @RequestMapping("queryAllByUserIdAndState")
    public ResultEntity queryAllByUserIdAndState(String userId,String state){
        List<CartVO> list =  myorderService.queryAllByUserIdAndState(userId,state);
        if (Objects.equals(list,null)){
            return ResultEntity.fail("查询失败");
        }
        return ResultEntity.success(list);
    }

    @RequestMapping("deleteItem")
    public ResultEntity deleteItemById(String id){
        boolean flag = myorderService.deleteById(Integer.parseInt(id));
        if (!flag){
            return ResultEntity.fail("删除失败");
        }
        return ResultEntity.success("删除成功");
    }

    @RequestMapping("pay")
    public ResultEntity pay(String id,String me,String money,String count,String ticket){
        boolean flag;
        if (money.isEmpty() || id.isEmpty() || count.isEmpty() || ticket.isEmpty()) return ResultEntity.fail("系统异常");
        if (Objects.equals(me,"me")){
             flag = myorderService.payMe(Integer.parseInt(id),Double.parseDouble(money),Integer.parseInt(count),ticket);
        }else{ // 不用本地余额的抵用券逻辑没写
            flag = myorderService.pay(Integer.parseInt(id),Double.parseDouble(money),Integer.parseInt(count));
        }
        if (!flag){
            return ResultEntity.fail("修改失败");
        }
        return ResultEntity.success("修改成功");
    }

    @RequestMapping("getIt")
    public ResultEntity getIt(String id){
        boolean flag = true;
        if (!id.isEmpty()){
            flag = myorderService.getIt(id);
        }
        if (!flag){
            return ResultEntity.fail("修改失败");
        }
        return ResultEntity.success("修改成功");
    }



}